/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.canvaschart.client.test;

import net.kornr.abstractcanvas.client.CanvasPainter;
import net.kornr.abstractcanvas.client.ICanvasExt;
import net.kornr.canvaschart.client.background.BackgroundFrame;
import net.kornr.canvaschart.client.background.BackgroundColor;
import net.kornr.canvaschart.client.background.BackgroundGrid;
import net.kornr.canvaschart.client.xy.DefaultXYDataSet;
import net.kornr.canvaschart.client.xy.DefaultXYPlotter;
import net.kornr.canvaschart.client.xy.XYBarRenderer;
import net.kornr.canvaschart.client.xy.XYLineRenderer;


public class SimpleXYPlot implements CanvasPainter 
{
	private int m_type;
	
	public SimpleXYPlot(int type)
	{
		m_type = type;
	}

	public void drawCanvas(ICanvasExt canvas) 
	{
		switch(m_type)
		{
		case 0:
			plotRandom(canvas);
			break;
		case 1:
			plotRandomBar(canvas);
			break;
		}
	}

	
	static public void plotRandom(ICanvasExt canvas)
	{	
		DefaultXYDataSet dataset = new DefaultXYDataSet();
		double previous = Math.random()*100;
		for (int i=0; i<300; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset.add((double)i, previous);
		}

		DefaultXYDataSet dataset2 = new DefaultXYDataSet();
		previous = Math.random()*100;
		for (int i=0; i<300; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset2.add((double)i, previous);
		}

		DefaultXYDataSet dataset3 = new DefaultXYDataSet();
		previous = Math.random()*100;
		for (int i=0; i<300; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset3.add((double)i, previous);
		}

		DefaultXYPlotter plotter = new DefaultXYPlotter(canvas);
		plotter.getCoordinateSystem().prepare(dataset);
		plotter.getCoordinateSystem().prepare(dataset2);
		// plotter.getCoordinateSystem().prepare(dataset3);
		plotter.getCoordinateSystem().addVerticalBorder();
		
		plotter.addBackground(new BackgroundColor("#FEFEFE"));
		plotter.addBackground(new BackgroundGrid());
		plotter.addBackground(new BackgroundFrame("#000000", 3));
		
		XYLineRenderer xlr;
		plotter.add(new XYLineRenderer(dataset, 1.2d, "rgb(0,0,0)", "rgb(100,255,100)"));
		plotter.add(xlr = new XYLineRenderer(dataset2, 3d, "rgb(255,0,0)"));
		xlr.setEnableAnnotation(false);
		// plotter.add(new XYLineRenderer(dataset3, 1.2d, "rgb(0,0,255)", "rgb(100,100,255)"));
		
		plotter.plot();
	}
	
	static public void plotRandomBar(ICanvasExt canvas)
	{	
		DefaultXYDataSet dataset = new DefaultXYDataSet();
		double previous = Math.random()*100;
		for (int i=0; i<15; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset.add((double)i, previous);
		}

		DefaultXYDataSet dataset2 = new DefaultXYDataSet();
		previous = Math.random()*100;
		for (int i=0; i<15; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset2.add((double)i, previous);
		}

		DefaultXYDataSet dataset3 = new DefaultXYDataSet();
		previous = Math.random()*100;
		for (int i=0; i<15; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset3.add((double)i, previous);
		}

		DefaultXYDataSet dataset4 = new DefaultXYDataSet();
		previous = Math.random()*100;
		for (int i=0; i<15; i++)
		{
			previous = previous + (Math.random()*30)-15;
			dataset4.add((double)i, previous);
		}

		DefaultXYPlotter plotter = new DefaultXYPlotter(canvas);
		// DefaultXYPlotter plotter = new AnimatedXYPlotter(canvas);
		
		plotter.getCoordinateSystem().prepare(dataset);
		plotter.getCoordinateSystem().prepare(dataset2);
		plotter.getCoordinateSystem().prepare(dataset3);
		plotter.getCoordinateSystem().prepare(dataset4);

		plotter.getCoordinateSystem().addVerticalBorder();
		
		plotter.addBackground(new BackgroundColor("#EFEFEF"));
		plotter.addBackground(new BackgroundGrid(14,15));
		XYBarRenderer bar = new XYBarRenderer();

		bar.addDataSet(dataset, "rgb(0,0,0)", "rgb(100,100,255)");
		bar.addDataSet(dataset2, "rgb(0,0,0)", "rgb(255,100,100)");
		//bar.addDataSet(dataset3,"rgb(0,0,0)", "rgb(100,255,100)");
		
		plotter.add(bar);
		XYLineRenderer xlr;
		plotter.add(xlr = new XYLineRenderer(dataset4, 1.2d, "rgb(0,0,255)", "rgb(100,100,255)"));
		xlr.setEnableAnnotation(false);
		plotter.plot();
	}
	
}
